package com.sixi.GeoTransfer;


import java.awt.Point;
import java.awt.geom.Point2D;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import com.sixi.GeoTransfer.model.AreaModel;
import com.sixi.GeoTransfer.model.City;
import com.sixi.GeoTransfer.model.Province;

import static com.sixi.GeoTransfer.Constants.*;


public class LoadAreaInfo {

	
	public static List<AreaModel> loadProvincesInfo() {
		
		String filePath = CHINA_PROVINCE_FILE_PATH;
		
		File f = new File(filePath);
		BufferedReader br = null;
		List<AreaModel> provinceList = new ArrayList<AreaModel>();
		
		System.out.println("读取省份经纬度信息");
		try {
			if(f.exists()) {
				//Eclipse调试环境下
				br = new BufferedReader(new FileReader(f));
			}else {
				//jar包环境下
				br =new BufferedReader( new InputStreamReader(ClassLoader.getSystemResourceAsStream(CHINA_PROVINCE_FILE_PATH)));
			}
			String temp;
			while ((temp = br.readLine()) != null) {
				String[] districtInfoArray = temp.split("\\t");
				if(districtInfoArray.length == DISTRICT_INFO_COL_NUM) {
					if(districtInfoArray[4].equals(LOCATION_LEVEL_PROVINCE)) {
						Province province = new Province();
						province.setAdCode(districtInfoArray[1]);//邮政编码
						province.setCityCode(districtInfoArray[2]);//区号
						
						String[] ctPoint = districtInfoArray[3].split(",");//区域中心点
						if(2 == ctPoint.length) {
							Point2D.Double ctp = new Point2D.Double(Double.parseDouble(ctPoint[0]),Double.parseDouble(ctPoint[1]));
							province.setCentre(ctp);
						}
						
						province.setAreaLevel(LOCATION_LEVEL_PROVINCE);//区域等级，province。
						province.setCountry(districtInfoArray[5]);//国家
						province.setName(districtInfoArray[6]);//省份名称
						
						String[] points = districtInfoArray[8].split(";"); //边界点多边形
						List<Point2D.Double> polyLine = new ArrayList<Point2D.Double>();
						if(points.length > 2) {//大于2才能组成多边形
							for(String pt : points) {
								String[] ptSpli = pt.split(",");
								if(ptSpli.length == 2) {
									Point.Double vertex = new Point.Double(Double.parseDouble(ptSpli[0]), Double.parseDouble(ptSpli[1])) ;//顶点
									polyLine.add(vertex);
								}//if
							}//for
						}//if
						province.setPolyLine(polyLine);
						
						provinceList.add(province);
					}//if
				}//if
			}//while
			
			br.close();
		
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}//catch
		
		
		return provinceList;
	}//loadProvincesInfo
	
	
	public static List<AreaModel> loadCityInfo() {
		
		String filePath = CHINA_CITY_FILE_PATH;
		
		File f = new File(filePath);
		BufferedReader br = null;
		List<AreaModel> cityList = new ArrayList<AreaModel>();
		
		System.out.println("读取城市经纬度信息");
		try {
			if(f.exists()) {
				//Eclipse调试环境下
				br = new BufferedReader(new FileReader(f));
			}else {
				//jar包环境下
				br =new BufferedReader( new InputStreamReader(ClassLoader.getSystemResourceAsStream(CHINA_CITY_FILE_PATH)));
			}
			String temp;
			while ((temp = br.readLine()) != null) {
				String[] districtInfoArray = temp.split("\\t");
				if(districtInfoArray.length == DISTRICT_INFO_COL_NUM) {
					if(districtInfoArray[4].equals(LOCATION_LEVEL_CITY)) {
						City city = new City();
						city.setAdCode(districtInfoArray[1]);//邮政编码
						city.setCityCode(districtInfoArray[2]);//区号
						
						String[] ctPoint = districtInfoArray[3].split(",");//区域中心点
						if(2 == ctPoint.length) {
							Point2D.Double ctp = new Point2D.Double(Double.parseDouble(ctPoint[0]),Double.parseDouble(ctPoint[1]));
							city.setCentre(ctp);
						}
						
						city.setAreaLevel(LOCATION_LEVEL_CITY);//区域等级，province。
						city.setCountry(districtInfoArray[5]);//国家
						city.setProvince(districtInfoArray[6]);//省份名称
						city.setName(districtInfoArray[7]);
						
						String[] points = districtInfoArray[8].split(";"); //边界点多边形
						List<Point2D.Double> polyLine = new ArrayList<Point2D.Double>();
						if(points.length > 2) {//大于2才能组成多边形
							for(String pt : points) {
								String[] ptSpli = pt.split(",");
								if(ptSpli.length == 2) {
									Point.Double vertex = new Point.Double(Double.parseDouble(ptSpli[0]), Double.parseDouble(ptSpli[1])) ;//顶点
									polyLine.add(vertex);
								}//if
							}//for
						}//if
						city.setPolyLine(polyLine);
						
						cityList.add(city);
					}//if
				}//if
			}//while

			br.close();
		
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}//catch
		
		return cityList;
	}//loadCityInfo
	
	
	
	public static List<AreaModel> loadAreaInfo() {
		
		String filePath = CHINA_CITY_FILE_PATH;
		
		File f = new File(filePath);
		BufferedReader br = null;
		List<AreaModel> cityList = new ArrayList<AreaModel>();
		
		System.out.println("读取城市经纬度信息");
		try {
			if(f.exists()) {
				//Eclipse调试环境下
				br = new BufferedReader(new FileReader(f));
			}else {
				//jar包环境下
				br =new BufferedReader( new InputStreamReader(ClassLoader.getSystemResourceAsStream(CHINA_CITY_FILE_PATH)));
			}
			String temp;
			while ((temp = br.readLine()) != null) {
				String[] districtInfoArray = temp.split("\\t");
				if(districtInfoArray.length == DISTRICT_INFO_COL_NUM) {
					if(districtInfoArray[4].equals(LOCATION_LEVEL_CITY)) {
						City city = new City();
						city.setAdCode(districtInfoArray[1]);//邮政编码
						city.setCityCode(districtInfoArray[2]);//区号
						
						String[] ctPoint = districtInfoArray[3].split(",");//区域中心点
						if(2 == ctPoint.length) {
							Point2D.Double ctp = new Point2D.Double(Double.parseDouble(ctPoint[0]),Double.parseDouble(ctPoint[1]));
							city.setCentre(ctp);
						}
						
						city.setAreaLevel(LOCATION_LEVEL_CITY);//区域等级，province。
						city.setCountry(districtInfoArray[5]);//国家
						city.setProvince(districtInfoArray[6]);//省份名称
						city.setName(districtInfoArray[7]);
						
						String[] points = districtInfoArray[8].split(";"); //边界点多边形
						List<Point2D.Double> polyLine = new ArrayList<Point2D.Double>();
						if(points.length > 2) {//大于2才能组成多边形
							for(String pt : points) {
								String[] ptSpli = pt.split(",");
								if(ptSpli.length == 2) {
									Point.Double vertex = new Point.Double(Double.parseDouble(ptSpli[0]), Double.parseDouble(ptSpli[1])) ;//顶点
									polyLine.add(vertex);
								}//if
							}//for
						}//if
						city.setPolyLine(polyLine);
						
						cityList.add(city);
					}//if
				}//if
			}//while

			br.close();
		
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}//catch
		
		return cityList;
	}//loadAreaInfo
	
	
	public static List<City> loadCityInfo(String provinceName) {
		
		//Eclipse调试环境下
		String filePath= ClassLoader.getSystemResource( CHINA_CITY_FILE_PATH).getPath();
		File f = new File(filePath);
		BufferedReader br = null;
		List<City> cityList = new ArrayList<City>();
		
		System.out.println("读取城市经纬度信息");
		try {
			
			if(f.exists()) {
				//Eclipse调试环境下
				br = new BufferedReader(new FileReader(f));
			}else {
				//jar包环境下
				br =new BufferedReader( new InputStreamReader(ClassLoader.getSystemResourceAsStream(CHINA_CITY_FILE_PATH)));
			}
			String temp;
			while ((temp = br.readLine()) != null) {
				String[] districtInfoArray = temp.split("\\t");
				if(districtInfoArray.length == DISTRICT_INFO_COL_NUM) {
						if(districtInfoArray[4].equals(LOCATION_LEVEL_CITY)) {
							City city = new City();
							city.setAdCode(districtInfoArray[1]);//邮政编码
							city.setCityCode(districtInfoArray[2]);//区号
							
							String[] ctPoint = districtInfoArray[3].split(",");//区域中心点
							if(2 == ctPoint.length) {
								Point2D.Double ctp = new Point2D.Double(Double.parseDouble(ctPoint[0]),Double.parseDouble(ctPoint[1]));
								city.setCentre(ctp);
							}
							
							city.setAreaLevel(LOCATION_LEVEL_CITY);//区域等级，province。
							city.setCountry(districtInfoArray[5]);//国家
							city.setProvince(districtInfoArray[6]);//省份名称
							city.setName(districtInfoArray[7]);
							
							String[] points = districtInfoArray[8].split(";"); //边界点多边形
							List<Point2D.Double> polyLine = new ArrayList<Point2D.Double>();
							if(points.length > 2) {//大于2才能组成多边形
								for(String pt : points) {
									String[] ptSpli = pt.split(",");
									if(ptSpli.length == 2) {
										Point.Double vertex = new Point.Double(Double.parseDouble(ptSpli[0]), Double.parseDouble(ptSpli[1])) ;//顶点
										polyLine.add(vertex);
									}//if
								}//for
							}//if
							city.setPolyLine(polyLine);
							//如果省份名不为空则加载全部
							if(provinceName!=null){
								if(provinceName.equals(districtInfoArray[6])){
									cityList.add(city);
								}
							}else {
								cityList.add(city);
							}
							
						}//if
						
					
				}//if
			}//while

			br.close();
		
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}//catch
		
		return cityList;
	}//loadCityInfo
	
	
}//LoadAreaInfo.class
